import { register } from "@/api/user";
import { Button, Form, Input, Toast } from "@ant-design/react-native";
import { router } from "expo-router";
import { SafeAreaView } from "react-native-safe-area-context";

export default function SignUpScreen() {
  const [form] = Form.useForm();
  const handleSubmit = () => {
    form.submit();
  };

  const handleFinish = (values: {
    username: string;
    password: string;
    confirmPassword: string;
  }) => {
    register(values)
      .then((res) => {
        router.push("/sign_in");
      })
      .catch((err) => {
        Toast.fail(err);
      });
  };

  return (
    <SafeAreaView>
      <Form
        initialValues={{ username: "", password: "", confirmPassword: "" }}
        form={form}
        onFinish={handleFinish}
      >
        <Form.Item name="username">
          <Input placeholder="请输入用户名" />
        </Form.Item>
        <Form.Item name="password">
          <Input placeholder="请输入密码" secureTextEntry />
        </Form.Item>
        <Form.Item name="confirmPassword">
          <Input placeholder="请确认密码" secureTextEntry />
        </Form.Item>

        <Form.Item>
          <Button type="primary" onPress={handleSubmit}>
            注册
          </Button>
        </Form.Item>
      </Form>
    </SafeAreaView>
  );
}
